#pragma once

#include<iostream>
#include<mutex>
#include<thread>
using namespace std;
#include<stdio.h>
#include <direct.h> // Windows


enum
{
	SCREEN,ONEFILE,CLASSIFY
};
enum
{
	INFO = 10,DEBUG,MERROR,WARING,COUNT
};
class Log
{
	mutex mtx;
	Log() {}
public:
	static Log log;
	Log& operator=(const Log&) = delete;
	Log(const Log&) = delete;

	string GetFileName(int type)
	{
		string ret;
		switch (type)
		{
		case INFO:
			ret = "info.txt";
			break;
		case DEBUG:
			ret = "debug.txt";
			break;
		case ERROR:
			ret = "error.txt";
			break;
		case WARING:
			ret = "waring.txt";
			break;
		case COUNT:
			ret = "count.txt";
			break;
		}
		return ret;
	}
	string GetType(int type)
	{
		string ret;
		switch (type)
		{
		case INFO:
			ret = "[ info ]";
			break;
		case DEBUG:
			ret = "[ debug ]";
			break;
		case MERROR:
			ret = "[ error ]";
			break;
		case WARING:
			ret = "[ waring ]";
			break;
		case COUNT:
			ret = "[ count ]";
			break;
		}
		return ret;
	}
	void WriteToFile(int des,int type,const string& content)
	{
		string ret = GetType(type);
		ret += content;
		switch (des)
		{
		case SCREEN:
			cout << ret << endl;
			break;
		case CLASSIFY:
			string filename = GetFileName(type);

			{
				unique_lock<mutex> lg(mtx);
				FILE* fd = fopen(filename.c_str(), "a");
				char str[100];
				snprintf(str, sizeof(str), "%s\n", ret.c_str());
				fwrite(str, sizeof(char), strlen(str), fd);
				fclose(fd);
			}
			break;
		}
	}
};
Log Log::log;